﻿Imports System.ComponentModel
Imports Excepciones
Imports Negocio

Public Class ComboBoxExt
    Inherits ComboBox
    Implements IControlValidatable

    Private _firstlankitem As Boolean = False
    Public Property FirstBlankItem() As Boolean
        Get
            Return _firstlankitem
        End Get
        Set(ByVal value As Boolean)
            _firstlankitem = value
        End Set
    End Property


    Public Overloads Property DataSource() As Object
        Get
            Return MyBase.DataSource
        End Get
        Set(ByVal value As Object)
            If TypeOf value Is ICollection Then
                Me.Items.Clear()
                If _firstlankitem Then
                    Me.Items.Insert(0, "")
                End If
                For Each obj As Object In value
                    Me.Items.Add(obj)
                Next
            Else
                MyBase.DataSource = value
            End If
        End Set
    End Property


#Region "Validacion"

    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero antes de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingBefore(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    ''' <summary>
    ''' Se produce cuando el control de esta validando, pero luego de la validación por tipo
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ValidatingAfter(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

    Private _validations As New List(Of Utilidades.ValidationType)
    <Browsable(True), _
    EditorBrowsable(EditorBrowsableState.Always), _
    Category("Validation"), _
    Description("Valida el texto con las opciones seleccionadas"), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public Property Validation() As List(Of Utilidades.ValidationType)
        Get
            Return _validations
        End Get
        Set(ByVal value As List(Of Utilidades.ValidationType))
            _validations = value
        End Set
    End Property

    Public Sub ValidateInput() Implements IControlValidatable.ValidateInput
        Dim text As String = Me.Text
        If (Me.SelectedIndex = -1 Or Me.SelectedItem Is Nothing) Then
            text = ""
        End If
        For Each tipo As Utilidades.ValidationType In _validations
            Utilidades.Validations.Validate(tipo, text)
        Next
    End Sub

    Private Sub Me_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
        Dim frm As Base
        Try
            frm = DirectCast(Me.FindForm(), Base)
            frm.ResetErrorUI(sender)
            Try
                RaiseEvent ValidatingBefore(sender, e)
                ValidateInput()
                RaiseEvent ValidatingAfter(sender, e)
            Catch ex As CustomException
                e.Cancel = True
                frm.SetErrorUI(sender, ex)
            End Try
        Catch ex As Exception

        End Try

    End Sub
#End Region

End Class
